import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/login',
  },
  {
    path: '/login',
    component: ()=>import('@/components/login.vue'),
  },
  {
    path: '/home',
    component: ()=>import('@/components/home.vue'),
    redirect: '/home/welcome',
    children: [
      {
        path: 'welcome',
        component: ()=>import('@/components/welcome.vue'),
      },
      {
        path: 'users',
        component: ()=>import('@/components/user/user.vue'),
      },
      {
        path: 'roles',
        component: ()=>import('@/components/power/roles.vue'),
      },
      {
        path: 'rights',
        component: ()=>import('@/components/power/rights.vue'),
      },
      {
        path: 'categories',
        component: ()=>import('@/components/goods/cate.vue'),
      },
      {
        path: 'params',
        component: ()=>import('@/components/goods/params.vue'),
      },
      {
        path: 'goods',
        component: ()=>import('@/components/goods/goods.vue'),
      },
      {
        path: 'goods/add',
        component:()=>import('@/components/goods/addgood/AddGoods.vue')
      },
      {
        path: 'orders',
        component:()=>import('@/components/order/order.vue')
      }, {
        path: 'reports',
        component:()=>import('@/components/chart/chart.vue')
      }
    ],
  },
]

const router = new VueRouter({
  routes,
})

router.beforeEach((to, from, next) => {
  // 获取token
  let token = sessionStorage.getItem('token')
  // 去登录页允许跳转
  if (to.path == '/login') {
    return next()
  }
  // 如果有token 允许跳转，没有token 强制跳转到登录页。
  if (token) {
    next()
    return
  } else {
    next('/login')
    return
  }
})

export default router
